import numpy as np


for i in range(0, 4):
    print(i)


print(np.random.random());

a = [2, 4, 6, 7, 8];
print(a);

line = '1,2,4,5,12 2,6,78,23,78 23,45,78,98,12';
line_1 = line.split();
print(line_1);
bboxes = np.array([list(map(int, box.split(','))) for box in line_1]);
print(bboxes);
print(type(bboxes));
print(bboxes[:, [0, 2]]);


def load_annotations(annot_path):
    with open(annot_path, 'r') as f:
        txt = f.readlines()
        annotations = [line.strip() for line in txt if len(line.strip().split()[1:]) != 0]
        annotations_1 = [line.strip().split() for line in txt];
        print(annotations_1);
        print(type(annotations_1[0]));
    np.random.shuffle(annotations)
    return annotations

annotations_path = 'F:/new/CSDN/实践项目/yolo_v3/tensorflow-yolov3/data/dataset/voc_train.txt';

annotations_list = load_annotations(annotations_path);
print(annotations_list);
print(type(annotations_list));

print(np.ceil(12 / 5))

import random
strides = [8, 16, 32];
anchor_per_scale = 3;
num_classes = 5;
train_input_sizes = [320, 352, 384, 416, 448, 480, 512, 544, 576, 608];
choice_input = random.choice(train_input_sizes);
#print(choice_input)
train_output_sizes = choice_input // np.array(strides);

print(np.array(strides))
label = [np.zeros((3, 3, anchor_per_scale,
                           5 + num_classes)) for i in range(3)];
print('232323===========23232323');
print(label[0].shape, label[1].shape, label[2].shape);

max_bbox_per_scale = 150;
bboxes_xywh = [np.zeros((max_bbox_per_scale, 4)) for _ in range(3)]
print(len(bboxes_xywh));

bbox_count = np.zeros((3,))
print(bbox_count)

uniform_distribution = np.full(num_classes, 1.0 / num_classes);
print(uniform_distribution);

deta = 0.01
onehot = np.zeros(num_classes, dtype=np.float)
onehot[1] = 1.0
smooth_onehot = onehot * (1 - deta) + deta * uniform_distribution;
print(smooth_onehot);

coor = [4, 8, 9, 20];
coor_np = np.array(coor);
print(type(coor_np))
print(coor_np.shape);
bbox_xywh = np.concatenate([(coor_np[2:] + coor_np[:2]) * 0.5, coor_np[2:] - coor_np[:2]], axis=-1);
print(bbox_xywh);
print(bbox_xywh[np.newaxis, :].shape);
strides_1 = np.array(strides)[:, np.newaxis];
print(strides_1);

bbox_xywh_scaled = 1.0 * bbox_xywh[np.newaxis, :] / strides_1;
print(bbox_xywh_scaled);
print('=================================');
left_up = np.maximum(bbox_xywh_scaled[0, :2], bbox_xywh_scaled[..., :2]);
print(bbox_xywh_scaled[0, :2]);
print(bbox_xywh_scaled[..., :2]);
print(left_up);
right_down = np.minimum(bbox_xywh_scaled[..., 2:], bbox_xywh_scaled[..., 2:]);
inter_section = np.maximum(right_down - left_up, 0.0);
print('inter_section', inter_section);
print(inter_section.shape);
inter_area = inter_section[..., 0] * inter_section[..., 1]
print('inter_area', inter_area);
print(inter_area.shape);
inter_area_1 = inter_area;
inter_area_2 = inter_area;
inter_area[0] = 0.2;
inter_area[1] = 0.4;
inter_area[2] = 0.6;
inter_area_1[0] = 0.5;
inter_area_1[1] = 0.8;
inter_area_1[2] = 0.3;
inter_area_2[0] = 0.76;
inter_area_2[1] = 0.48;
inter_area_2[2] = 0.33;
print('inter_area', inter_area);
print('=======---------=======');
yind = 1; xind = 2;
iou_mask = inter_area > 0.45;
label[1][yind, xind, iou_mask, :] = 5;
print(label[1].shape);
print(label[1]);
print('iou_mask:', iou_mask);
print(type(iou_mask));
print(iou_mask.dtype);
print('=======---------=======');
iou_list = [];
iou_list.append(inter_area);
iou_list.append(inter_area_1);
iou_list.append(inter_area_2);
best_anchor_ind = np.argmax(np.array(iou_list).reshape(-1), axis=-1);
print(np.array(iou_list));
print(np.array(iou_list).reshape(-1));
print('best_anchor_ind: ', best_anchor_ind);
print('=======---------=======');
anchors_path = 'F:/new/CSDN/实践项目/yolo_v3/tensorflow-yolov3/data/anchors/basline_anchors.txt'

def get_anchors(anchors_path):
    '''loads the anchors from a file'''
    with open(anchors_path) as f:
        anchors = f.readline()
    anchors = np.array(anchors.split(','), dtype=np.float32)
    return anchors.reshape(3, 3, 2)

winder_anchors = get_anchors(anchors_path);
print(winder_anchors);

print(1 % 150);

import tensorflow as tf
output_size = 20;batch_size = 5;
y = tf.tile(tf.range(output_size, dtype=tf.int32)[:, tf.newaxis], [1, output_size]);
x = tf.tile(tf.range(output_size, dtype=tf.int32)[tf.newaxis, :], [output_size, 1]);
xy_grid = tf.concat([x[:, :, tf.newaxis], y[:, :, tf.newaxis]], axis=-1);
xy_grid = tf.tile(xy_grid[tf.newaxis, :, :, tf.newaxis, :], [batch_size, 1, 1, anchor_per_scale, 1]);
xy_grid = tf.cast(xy_grid, tf.float32);
eee = tf.exp(xy_grid);
with tf.Session() as sess:
    output = sess.run(y);
    xy_output = sess.run(xy_grid);
    widner_ee = sess.run(eee);
    #print(output.shape);
    #print(output)
    print(widner_ee.shape);
    print(widner_ee);

    print(xy_output.shape);
    print(xy_output);